package org.helian.controller;

import com.sun.deploy.net.HttpResponse;
import org.apache.commons.lang.StringUtils;
import org.helian.pojo.TbUser;
import org.helian.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * @Description TODO
 * @Author 墨染
 * @Date 2020/11/7 17:46
 * @Version 1.0
 */
@RequestMapping("/user")
@Controller
public class UserController {

    @Resource(name = "userServiceImpl")
    private UserService userService;

    // key为用户id，value为用户登录时session(同一浏览器session相同)
    public static HashMap<String, HttpSession> sessionMap = new HashMap<>();

    @Autowired
    private RedisTemplate redisTemplate;

    /***
     * 跳转登录界面
     * @return
     * @throws Exception
     */
    @GetMapping("/loginPage")
    public String loginPage(Model model) throws Exception {
        model.addAttribute("a","a");
        return "login";
    }

    /***
     * 登录
     * @param username：用户名
     * @param password：密码
     * @param session：当前浏览器的Session
     * @param remember: 记住我 1
     * @return
     * @throws Exception
     */
    @PostMapping("/doLogin")
    public String doLogin(String username, String password, String remember, HttpSession session, HttpServletResponse response) throws Exception {
        if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
            TbUser user = userService.findByUsernameAndPassword(username, password);
            if (user != null) {
                // 添加本次登录用户的sessionMap,当前userId已有记录则覆盖
                String userId = String.valueOf(user.getId());
                session.setAttribute("user", user);
                sessionMap.put(userId, session);
                redisTemplate.opsForValue().set("login_key", userId);
                //记住我
                if (StringUtils.isNotBlank(remember) && "1".equals(remember)) {
                    Cookie userCookie = new Cookie("dormuser", user.getUsername() + "-" + user.getPassword());
                    userCookie.setMaxAge(1 * 60 * 60 * 24 * 7); //cookie过期时间
                    response.addCookie(userCookie);
                }
                return "redirect:/user/toSuccess";
            }
        }
        return "redirect:/user/loginPage";
    }

    /***
     * 登录成功
     * @return
     * @throws Exception
     */
    @GetMapping("/toSuccess")
    public String toSuccess() throws Exception {
        return "success";
    }

    /***
     * 注销
     * @param req
     * @return
     * @throws Exception
     */
    @GetMapping("/toLogin")
    public String login(HttpServletRequest req) throws Exception {
        TbUser user = (TbUser) req.getSession().getAttribute("user");
        if (user != null) {
            UserController.sessionMap.remove(String.valueOf(user.getId()));
            req.getSession().removeAttribute("user");
        }
        return "redirect:/user/loginPage";
    }

    /***
     * 测试：用户列表界面
     * 前提：必须是登录之后才能进得去
     * @return
     */
    @GetMapping("/toUserList")
    public String toUserList() {
        return "userlist";
    }
}
